/**
 * 全局混入，注意使用！ 一旦使用全局混入对象，将会影响到所有之后创建的Vue实例。
 */
import { format, isNotEmpty } from '@/commons/utils';
import constants from '@/commons/constants';
import { constant } from 'lodash';

export default {
    computed: {
        isDisabled: function () {
            return function (obj) {
                let result = false;
                for (const key of Object.keys(obj)) {
                    if (!obj[key]) {
                        result = true;
                        break;
                    }
                }
                return result;
            };
        },
        formatTime() {
            return (time, type) => format(time, type);
        },
        transformStream() {
            return (val, key) => {
                if (isNotEmpty(constants[key])) {
                    return constants[key][val];
                } else {
                    throw new Error('常量对象不存在');
                }
            };
        },
        priceFilter() {
            return (val = 0, digit = 2) => {
                return new Intl.NumberFormat('zh-CN', {
                    maximumFractionDigits: digit,
                    minimumFractionDigits: digit
                }).format(Number(val));
            };
        }
    },
    methods: {
        /**
         * 拦截Submit按钮
         * @param {*} bool
         * @param {*} callback
         */
        btnInterceptor(bool, callback) {
            if (!bool) {
                callback();
            }
        },
        exortExc(res, name, fileType) {
            const blob = new Blob([res.data], {
                type: 'application/msexcel'
            });
            const url = window.URL.createObjectURL(blob);
            const link = document.createElement('a');
            link.href = url;
            let fileName = fileType ? name + '.' + fileType : name + '.xlsx';
            if (!res.headers) {
                const contentDisposition = res.headers['content-disposition'];
                const fileNameMatch = contentDisposition.split('filename=');
                if (fileNameMatch.length === 2) {
                    const temp = fileNameMatch[1].replace(new RegExp('\\+', 'gm'), '%20');
                    fileName = decodeURIComponent(temp);
                }
            }
            link.setAttribute('download', fileName);
            document.body.appendChild(link);
            link.click();
            link.remove();
            window.URL.revokeObjectURL(url);
        }
    },
    created() {}
};
